package com.xiaoshuidi.cloud.module.finance.controller.admin.invoice;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.framework.excel.core.util.ExcelUtils;
import com.xiaoshuidi.cloud.framework.operatelog.core.annotations.OperateLog;
import com.xiaoshuidi.cloud.framework.security.core.LoginUser;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.contract.vo.OrderInvoicePageRespVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.flow.FinanceMoneyFlowExcelVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.invoice.vo.*;
import com.xiaoshuidi.cloud.module.finance.controller.admin.merchant.vo.merchant.PayMerchantExcelVO;
import com.xiaoshuidi.cloud.module.finance.convert.finance.FinanceConvert;
import com.xiaoshuidi.cloud.module.finance.dal.mysql.invoice.BaseInvoiceMapper;
import com.xiaoshuidi.cloud.module.finance.service.invoice.FinanceInvoiceService;
import com.xiaoshuidi.cloud.module.finance.service.invoice.BaseInvoiceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.io.IOException;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;
import static com.xiaoshuidi.cloud.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "财务管理 - 开票管理")
@RestController
@RequestMapping("/finance/invoiceManage")
public class FinanceInvoiceController {
    @Resource
    private FinanceInvoiceService financeInvoiceService;
    @Resource
    private BaseInvoiceService baseInvoiceService;
    @Resource
    private BaseInvoiceMapper invoiceMapper;


    @GetMapping("/getInvoiceListByEntity")
    @Operation(summary = "开票管理--个人")
    @PermitAll
    public CommonResult<PageResult<OrderInvoicePageRespVO>> getInvoiceListByEntity(FinanceInvoicePageReqVO vo) {
        vo.setRoommateId(0);
        PageResult<OrderInvoicePageRespVO> data = financeInvoiceService.getInvoiceList(vo);
        return success(data);
    }
//    /**
//     * 导出开票管理--个人
//     * */
//    @GetMapping("/exportInvoiceListByEntity")
//    @Operation(summary = "导出开票管理--个人 Excel")
//    @OperateLog(type = EXPORT)
//    public void exportInvoiceListByEntity(FinanceInvoiceReqVO vo, HttpServletResponse response) throws IOException {
//        vo.setRoommateId(0);
//        List<BaseInvoiceExcelVO> list = financeInvoiceService.getExportInvoiceList(vo);
//        ExcelUtils.write(response, "开票管理个人.xlsx", "数据", BaseInvoiceExcelVO.class, list);
//    }
    /**
     * 导出开票管理--个人
     * */
    @GetMapping("/exportInvoiceListByEntity")
    @Operation(summary = "导出开票管理--个人 Excel")
    @OperateLog(type = EXPORT)
    @PermitAll
    public void exportInvoiceListByEntity(FinanceInvoicePageReqVO vo, HttpServletResponse response) throws IOException {
        vo.setRoommateId(0);
        PageResult<OrderInvoicePageRespVO> pageResult = financeInvoiceService.getInvoiceList(vo);
        ExcelUtils.write(response, "开票管理个人.xlsx", "数据", OrderInvoicePageRespExcelVO.class, pageResult,
                FinanceConvert.INSTANCE::exportInvoiceListByEntity, (pageNo) -> {
                    vo.setPageNo(pageNo);
                    return financeInvoiceService.getInvoiceList(vo);
                });
    }

    @GetMapping("/getInvoiceListByFirm")
    @Operation(summary = "开票管理--企业")
    public CommonResult<PageResult<OrderInvoicePageRespVO>> getInvoiceListByFirm(FinanceInvoicePageReqVO vo) {
        vo.setRoommateId(1);
        PageResult<OrderInvoicePageRespVO> data = financeInvoiceService.getInvoiceList(vo);
        return success(data);
    }
//    /**
//     * 导出开票管理--企业
//     * */
//    @GetMapping("/exportInvoiceListByFirm")
//    @Operation(summary = "导出开票管理--企业 Excel")
//    @OperateLog(type = EXPORT)
//    public void exportInvoiceListByFirm(FinanceInvoiceReqVO vo, HttpServletResponse response) throws IOException {
//        vo.setRoommateId(1);
//        List<BaseInvoiceExcelVO> list = financeInvoiceService.getExportInvoiceList(vo);
//        ExcelUtils.write(response, "开票管理企业.xlsx", "数据", BaseInvoiceExcelVO.class, list);
//    }
    /**
     * 导出开票管理--企业
     * */
    @GetMapping("/exportInvoiceListByFirm")
    @Operation(summary = "导出开票管理--企业 Excel")
    @OperateLog(type = EXPORT)
    @PermitAll
    public void exportInvoiceListByFirm(FinanceInvoicePageReqVO vo, HttpServletResponse response) throws IOException {
        vo.setRoommateId(1);
        PageResult<OrderInvoicePageRespVO> pageResult = financeInvoiceService.getInvoiceList(vo);
        ExcelUtils.write(response, "开票管理企业.xlsx", "数据", OrderInvoicePageRespExcelVO.class, pageResult,
                FinanceConvert.INSTANCE::exportInvoiceListByEntity, (pageNo) -> {
                    vo.setPageNo(pageNo);
                    return financeInvoiceService.getInvoiceList(vo);
                });
    }


    @GetMapping("/changeInvoicesIssued")
    @Operation(summary = "标识已开票")
    @Parameter(name = "orderId", description = "账号id", required = true, example = "1024")
    public CommonResult<Boolean> changeInvoicesIssued(@RequestParam("orderId") Long orderId) {
        financeInvoiceService.changeInvoicesIssued(orderId);
        return success(true);
    }

    @PutMapping("/getDetailByOrderIds")
    @Operation(summary = "开票管理-根据账单Id获取明细准备开票")
    public CommonResult<OrderDetailInfoRespVO> getDetailByOrderIds(@RequestParam("orderIdList") List<Long> orderIdList) {
        OrderDetailInfoRespVO vo= baseInvoiceService.getDetailByOrderIds(orderIdList);
        return success(vo);
    }

    @PostMapping("/issueInvoices")
    @Operation(summary = "开票管理-单个/合并开票")
    @PermitAll
    public CommonResult<Void> issueInvoices(@Valid @RequestBody InvoiceIssueReqVO reqVO) {
        LoginUser user = SecurityFrameworkUtils.getLoginUser();
        baseInvoiceService.issueInvoices(user,reqVO);
        return CommonResult.success();
    }


    @GetMapping("/getInvoiceDetailByOrder")
    @Operation(summary = "开票管理-开票详情")
    @PermitAll
    public CommonResult<InvoiceInfoRespVO> getInvoiceDetailByOrder(@RequestParam("invoiceId") Long invoiceId) {
        InvoiceInfoRespVO vo = baseInvoiceService.getInvoiceDetailByOrder(invoiceId);
        return CommonResult.success(vo);
    }

    @GetMapping("/getIssueInvoiceList")
    @Operation(summary = "已开票管理--列表")
    @PermitAll
    public CommonResult<PageResult<IssueInvoicePageRespVO>> getIssueInvoiceList(IssueInvoicePageReqVO vo) {
        PageResult<IssueInvoicePageRespVO> data = baseInvoiceService.getIssueInvoiceList(vo);
        return success(data);
    }

    /**
     * 导出已开票管理
     * */
    @GetMapping("/exportIssueInvoiceList")
    @Operation(summary = "导出已开票管理 Excel")
    @OperateLog(type = EXPORT)
    @PermitAll
    public void exportIssueInvoiceList(IssueInvoicePageReqVO vo, HttpServletResponse response) throws IOException {
        PageResult<IssueInvoicePageRespVO> pageResult = baseInvoiceService.getIssueInvoiceList(vo);
        ExcelUtils.write(response, "已开票列表.xlsx", "数据", IssueInvoicePageRespExcelVO.class, pageResult,
                FinanceConvert.INSTANCE::exportIssueInvoiceList, (pageNo) -> {
                    vo.setPageNo(pageNo);
                    return baseInvoiceService.getIssueInvoiceList(vo);
                });
    }

    @GetMapping("/getInvoiceDetailByInvoice")
    @Operation(summary = "已开票管理-开票详情")
    public CommonResult<InvoiceInfoRespVO> getInvoiceDetailByInvoice(@RequestParam("invoiceId") Long invoiceId) {
        InvoiceInfoRespVO vo = baseInvoiceService.getInvoiceDetailByInvoice(invoiceId);
        return CommonResult.success(vo);
    }

    @GetMapping("/invalidateInvoice")
    @Operation(summary = "开票管理-作废")
    public CommonResult<Void> invalidateInvoice(@RequestParam("invoiceId") Long invoiceId) {
        LoginUser user = SecurityFrameworkUtils.getLoginUser();
        baseInvoiceService.invalidateInvoice(user,invoiceId);
        return CommonResult.success();
    }

    /**
     * 发票红冲
     * @param invoiceId
     * @return
     */
    @GetMapping("/invoiceRushRed")
    @Operation(summary = "开票管理-红冲")
    public CommonResult<String> invoiceRushRed(@RequestParam("invoiceId") Long invoiceId) {
        LoginUser user = SecurityFrameworkUtils.getLoginUser();
        return CommonResult.success(baseInvoiceService.invoiceRushRed(user,invoiceId));
    }

//    @PostMapping("/againInvoices")
//    @Operation(summary = "开票管理-换开发票")
//    public CommonResult<Void> againInvoices(@Valid @RequestBody InvoiceAgainReqVO reqVO) {
//        LoginUser user = SecurityFrameworkUtils.getLoginUser();
//        baseInvoiceService.issueInvoices(user,reqVO.getInvoiceIssueReqVO());
//        invoiceMapper.updateById(InvoiceDO.builder().id(reqVO.getOldInvoiceId()).status("6").build());
//        return CommonResult.success();
//    }



}
